import sys   
sys.path.append('code/feature_eng')
import os
from joblib import Parallel, delayed
import akshare as ak
import numpy as np
sys.path.append('code')
from data_capture.stock_capture_ak.get_data import get_stock_codes, get_price, get_market_value ,get_other_data
from data_capture.data_contrate import consentrate_price_value
import time
import shutil  

###数据更新
def catdata():
    cc = consentrate_price_value()
    cc.consentrate_columns()

def scrash_price():
    gz1000 = get_stock_codes(name='国证1000')['样本代码'].values
    gz2000 = get_stock_codes(name='国证2000')['样本代码'].values
    gz = np.union1d(gz1000, gz2000)
    kc = ak.stock_kc_a_spot_em()
    kc = kc['代码'].values
    codes = np.setdiff1d(gz,kc)
    lst = [i for i in codes if i[0] != '3']
    codes = lst
    try:
        shutil.rmtree(r'data\stock_data\daily')
        shutil.rmtree(r'data\cmodty\my_feature')
    except:
        print('已经移除因子文件')
    get_price(codes)
    get_market_value(codes)
    get_other_data()

# scrash_price()
# catdata()


###因子计算
from alpha_101.main_101 import calc_main_101, calc_main_alpha_ind9, calc_main_alpha_ind
from alpha_191.main_191 import calc_main_191 
from alpha_huangeven.main_huangeven import calc_main_even
from alpha_mine.main_mine import calc_main_mine
from feature_fundmantal.value_main import calc_main_fundmental
from alpha_series.alpha_main import calc_main_alpha_df, calc_main_alpha_ser
# calc_main_fundmental()
# calc_main_191()
calc_main_101()
# calc_main_alpha_ind9()
# calc_main_alpha_ind()
# calc_main_even()
# calc_main_mine()
# calc_main_alpha_df()
# calc_main_alpha_ser()


###因子检查
from feature_display import Feature_display, IC_display, Cor_display_fast, Cor_display_straitfy
import json

root_lst = [r'data\cmodty\my_feature\feature_data_df']
# root_lst = [r'data\cmodty\my_feature\feature_data_101']
# ic_dis = IC_display(start_date='2019-01-01', end_date='2022-11-01',feature_root=root_lst)
# ic_dis.info_generate()
# del ic_dis

# cor_dis = Cor_display_fast(end_date='2022-11-01',thred=0.8, feature_root=root_lst)
# cor_dis.cor_generate()

# cor_dis = Cor_display_straitfy(end_date='2022-11-01',thred=0.8, feature_root=root_lst)
# cor_dis.cor_generate()



def check_feature(check_root=None, feature_root=None):
    select_file = r'data\cmodty\check\feature_fliter_result.json'
    with open(select_file,'r', encoding='UTF-8') as f:
        select_dict = json.load(f)
    select_lst = select_dict['reamain']
    if check_root is None:
        check_root = r'data/cmodty/check/feature_data'
    if feature_root is None:
        feature_root = r'data/cmodty/my_feature/feature_data'
    lst = sorted(os.listdir(feature_root))
    def  check_func(i):
        if i[:-9] in select_lst:
            print(f'\n**********开始检查特征{i}')
            F = Feature_display(check_root, i, feature_root )
            F.test()
        # else:
        #     os.remove(os.path.join(feature_root, i))
            
    Parallel(n_jobs=16)(delayed(check_func)(i) for i in lst)
    # for i in lst:
    #     check_func(i)

# check_feature()
# check_feature(r'data/cmodty/check/feature_data_101', r'data/cmodty/my_feature/feature_data_101')
# check_feature(r'data/cmodty/check/feature_data_191', r'data/cmodty/my_feature/feature_data_191')
# check_feature(r'data/cmodty/check/feature_data_huangeven', r'data/cmodty/my_feature/feature_data_huangeven')
# check_feature(r'data/cmodty/check/feature_data_fd', r'data/cmodty/my_feature/feature_data_fd')

# check_feature(r'data/cmodty/check/feature_data_df', r'data\cmodty\my_feature\feature_data_df')
# check_feature(r'data/cmodty/check/feature_data_ser', r'data/cmodty/my_feature/feature_data_ser')